@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using BlazorApp.Pages.Event
@using BlazorApp.Pages.Pod
@using k8s.Models
@using Mapster
@inherits BlazorApp.Pages.Common.DrawerPageBase<V1Node>
@if (Node != null)
{
    <NodeAction Item="Node" MenuMode="MenuMode.Horizontal"></NodeAction>
    @if (_isMetricsServerReady)
    {
        <Divider Orientation="left" Style="font-weight:bold">@L["Metrics"]</Divider>
        <NodeStatusChart Node="@Node"></NodeStatusChart>
    }

    <Divider Orientation="left" Style="font-weight:bold">@L["Metadata"]</Divider>
    <MetadataView Item="Node.Metadata" ExplainFieldPrefix="node"></MetadataView>
    <Divider Orientation="left" Style="font-weight:bold">@L["Spec"]</Divider>
    <HTable Bordered Column="1">
        <TaintsView Taints="@Node.Spec.Taints"></TaintsView>
        <PropertySimpleView Title=@L["ConfigSource"] Item="@Node.Spec.ConfigSource" ExplainField="node.spec.configSource" ShowInJson="true"></PropertySimpleView>
        <PropertySimpleView Title=@L["ExternalID"] Item="@Node.Spec.ExternalID" ExplainField="node.spec.externalID"></PropertySimpleView>
        <PropertySimpleView Title=@L["PodCIDR"] Item="@Node.Spec.PodCIDR" ExplainField="node.spec.podCIDR"></PropertySimpleView>
        <PropertyListView Title=@L["PodCIDRs"] Items="@Node.Spec.PodCIDRs" ExplainField="node.spec.podCIDRs" T="string"></PropertyListView>
        <PropertySimpleView Title=@L["ProviderID"] Item="@Node.Spec.ProviderID" ExplainField="node.spec.providerID"></PropertySimpleView>
        <PropertySimpleView Title=@L["Unschedulable"] Item="@Node.Spec.Unschedulable" ExplainField="node.spec.unschedulable"></PropertySimpleView>
    </HTable>
    <Divider Orientation="left" Style="font-weight:bold">@L["Status"]</Divider>
    <HTable Bordered Column="1">
        <PropertyOptionView Title=@L["Phase"] Item="@Node.Status.Phase" ExplainField="node.status.phase" Options="@(new[] { "Pending", "Running", "Terminated" })"></PropertyOptionView>
        <ConditionsView Conditions="@(Node.Status.Conditions.Adapt<IList<V1Condition>>())"></ConditionsView>
        @if (Node.Status.Addresses is { Count: > 0 })
        {
            <HTableColumn Title=@L["Address"] ExplainField="node.status.addresses">
                @foreach (var addr in Node.Status.Addresses)
                {
                    <ColorfulTag>@addr.Type : @addr.Address</ColorfulTag>
                }
            </HTableColumn>
        }
        @if (Node.Status.DaemonEndpoints != null)
        {
            <HTableColumn Title=@L["DaemonEndpoints"] ExplainField="node.status.daemonEndpoints">
                <ColorfulTag > @($"KubeletEndpoint:{Node.Status.DaemonEndpoints.KubeletEndpoint.Port}")</ColorfulTag>
            </HTableColumn>
        }
        @if (Node.Status.NodeInfo != null)
        {
            <HTableColumn Title=@L["NodeInfo"] ExplainField="node.status.nodeInfo">
                <HTable>
                    <PropertySimpleView Title=@L["Architecture"] Item="@Node.Status.NodeInfo.Architecture" ExplainField="node.status.nodeInfo.architecture"></PropertySimpleView>
                    <PropertySimpleView Title=@L["BootID"] Item="@Node.Status.NodeInfo.BootID" ExplainField="node.status.nodeInfo.bootID"></PropertySimpleView>
                    <PropertySimpleView Title=@L["ContainerRuntimeVersion"] Item="@Node.Status.NodeInfo.ContainerRuntimeVersion" ExplainField="node.status.nodeInfo.containerRuntimeVersion"></PropertySimpleView>
                    <PropertySimpleView Title=@L["KernelVersion"] Item="@Node.Status.NodeInfo.KernelVersion" ExplainField="node.status.nodeInfo.kernelVersion"></PropertySimpleView>
                    <PropertySimpleView Title=@L["KubeProxyVersion"] Item="@Node.Status.NodeInfo.KubeProxyVersion" ExplainField="node.status.nodeInfo.kubeProxyVersion"></PropertySimpleView>
                    <PropertySimpleView Title=@L["KubeletVersion"] Item="@Node.Status.NodeInfo.KubeletVersion" ExplainField="node.status.nodeInfo.kubeletVersion"></PropertySimpleView>
                    <PropertySimpleView Title=@L["MachineID"] Item="@Node.Status.NodeInfo.MachineID" ExplainField="node.status.nodeInfo.machineID"></PropertySimpleView>
                    <PropertySimpleView Title=@L["OperatingSystem"] Item="@Node.Status.NodeInfo.OperatingSystem" ExplainField="node.status.nodeInfo.operatingSystem"></PropertySimpleView>
                    <PropertySimpleView Title=@L["OsImage"] Item="@Node.Status.NodeInfo.OsImage" ExplainField="node.status.nodeInfo.osImage"></PropertySimpleView>
                    <PropertySimpleView Title=@L["SystemUUID"] Item="@Node.Status.NodeInfo.SystemUUID" ExplainField="node.status.nodeInfo.systemUUID"></PropertySimpleView>
                </HTable>
            </HTableColumn>
        }
        <PropertySimpleView Title=@L["Config"] Item="@Node.Status.Config" ExplainField="node.status.config" ShowInJson="true"></PropertySimpleView>


        @if (Node.Status.Images is { Count: > 0 })
        {
            <HTableColumn Title=@L["Images"] ExplainField="node.status.images">
                <Collapse>
                    <Panel Key="1">
                        <HeaderTemplate>
                            <Badge Count="@Node.Status.Images.SelectMany(x => x.Names).Count()"/>
                        </HeaderTemplate>
                        <ChildContent>
                            @foreach (var image in Node.Status.Images)
                            {
                                <ul>
                                    @foreach (var name in image.Names)
                                    {
                                        <li >
                                            @(name) <ColorfulTag > @($"{image.SizeBytes / 1000 / 1000} MB")</ColorfulTag>
                                        </li>
                                    }
                                </ul>
                            }
                        </ChildContent>
                    </Panel>
                </Collapse>
            </HTableColumn>
        }


        @if (Node.Status.VolumesAttached is { Count: > 0 })
        {
            <HTableColumn Title=@L["VolumesAttached"] ExplainField="node.status.volumesAttached">
                <Collapse>
                    <Panel Key="1">
                        <HeaderTemplate>
                            <Badge Count="@Node.Status.VolumesAttached.Select(x => x.Name).Count()"/>
                        </HeaderTemplate>
                        <ChildContent>
                            @foreach (var attached in Node.Status.VolumesAttached)
                            {
                                <HTable>
                                    <HTableColumn Title=@L["Name"] ExplainField="node.status.volumesAttached.name">
                                        <div > @(attached.Name)</div>
                                    </HTableColumn>
                                    <PropertySimpleView Title=@L["DevicePath"] Item="@attached.DevicePath" ExplainField="node.status.volumesAttached.devicePath"></PropertySimpleView>
                                </HTable>
                            }
                        </ChildContent>
                    </Panel>
                </Collapse>
            </HTableColumn>
        }
        <PropertyListView Title=@L["VolumesInUse"] T="string" Items="@Node.Status.VolumesInUse" ExplainField="node.status.volumesInUse"></PropertyListView>
    </HTable>
    <NodeResourceCompareView Capacity="@Node.Status.Capacity" Allocatable="@Node.Status.Allocatable"></NodeResourceCompareView>
    <Divider Orientation="left" Style="font-weight:bold">@L["Metrics"]</Divider>
    <NodeMetricsSummaryView Node="@Node"></NodeMetricsSummaryView>

    <MiniPodListView Pods="_pods"></MiniPodListView>

    <MiniEventListView Host="@Node.Name()"></MiniEventListView>
}
